$(function(){
	//订单列表查询 
	selectOrderList();
});

//订单列表查询 
function selectOrderList(){
	$.ajax({
		type : "POST",
		url : "/order/orderList/search",
		async : false,
		success:function(data){
			var orderList = data.orderList;
			var $tbody = $("#orderList tbody");	
			if(orderList.length > 0) {
				for(var i=0;i<orderList.length;i++){
					var orderInfo = orderList[i];
					var payTime;
					if(orderInfo.payTime){
						payTime = new Date(orderInfo.payTime);
						payTime = toDate(payTime, "yyyy-MM-dd hh:mm:ss");
					}else{
						payTime = '';
					}
					var userEmailOrTell= orderInfo.userEmail;
					if(userEmailOrTell == null || userEmailOrTell == ''){
						userEmailOrTell = orderInfo.userTell;
					}
					var $tr = '<tr class="odd">';
					$tr += '<td>'+orderInfo.orderNum+'</td>';
					$tr += '<td>'+userEmailOrTell+'</td>';
					$tr += '<td>'+checkNull(orderInfo.belongCompanyName)+'</td>';
					$tr += '<td>'+checkNull(orderInfo.companyDomainName)+'</td>';
					$tr += '<td>'+checkNull(orderInfo.mealName)+'</td>';
					$tr += '<td>'+checkNull(orderInfo.payMoney)+'</td>';
					$tr += '<td>'+payTime+'</td>';
					$tr += '<td>'+setOrderState(orderInfo.orderState)+'</td>';
					$tr += '<td>'+setOrderType(orderInfo.isDredge)+'</td>';
					$tr += '<td><button type="button" class="btn btn-primary" onclick="toDetail('+orderInfo.orderId+');">详情</button>';
					if(orderInfo.orderState == 0 && orderInfo.buyType==0)
						$tr += '<button type="button" class="btn btn-danger" onclick="toRefund('+orderInfo.orderId+');">退单</button>';
					$tr += '</td></tr>';
					$tbody.append($tr);
				}
			}else {
				var $tr = '<tr class="odd"><td colspan="8">暂无数据！</td></tr>';
				$tbody.append($tr);
				alert("查询失败！")
			}
		}
	})
}

//详情界面
function toDetail(orderId){
	location.href="/order/orderDetail/"+orderId;
}

//退单操作
function toRefund (orderId){
	$.ajax({
		type : "POST",
		url : "/order/updateOrder",
		async : false,
		data:{
			orderId:orderId,
		},
		success:function(data){
			if(data) alert("退单成功！");
			else alert("退单失败！");
		}
	})
}

//检测非空
function checkNull(para){
	if(para == null){
		return "";
	}else{
		return para;
	}
}

 
//设置付款状态
function setOrderState(para){
	if (para == "0") {
		return "已付款";
	} else if (para == "1") {
		return "待付款";
	} else if(para == "2"){
		return "已取消";
	}
}

//设置开通状态
function setOrderType(para){
	if (para == "0") {
		return "未开通";
	} else if (para == "1") {
		return "已开通";
	}
}




